package com.example.test.conbase.base;


import android.os.Bundle;

import androidx.annotation.Nullable;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;

/**
 * Activity 基类
 *
 * @param <VM> ViewModel
 * @param <VDB> ViewDataBinding
 */

/**
 * Activity 基类
 *
 * @param <V> BaseView
 * @param <P> BasePresenter
 */
public abstract class BaseActivity<V extends BaseView,P extends BasePresenter<V>>
        extends RxAppCompatActivity {

    /**
     * 引用V层
     */
    private P presenter;
    /**
     * 引用P层
     */
    private V view;

    /**
     * 获取presenter对象
     *
     * @return
     */
    public P getPresenter(){
        return presenter;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        if(presenter == null){
            presenter = createPresenter();
        }
        if(view == null){
            view = createView();
        }
        if(presenter != null && view != null){
            presenter.attachView(view);
        }
        init();
    }

    /**
     * 获取布局文件
     *
     * @return 布局文件id
     */
    public abstract int getLayoutId();

    /**
     * 初始化Presenter
     * @return  Presenter对象
     */
    public abstract P createPresenter();

    /**
     * 初始化View对象
     * @return View对象
     */
    public abstract V createView();

    /**
     * 初始化数据
     */
    public abstract void init();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(presenter != null){
            presenter.detachView();
        }
    }
}
